#include common_scripts\utility;
#include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;
#include z_mod\utility;


medbox()
{
    level.zItem_MedBox_temp = spawn("script_model", self.origin);
    level.zItem_MedBox_temp setModel("tag_origin");
    level.zItem_MedBox_temp linkTo(self, "tag_weapon_right", (200, 0, 0), (0, 0, 0)); // работает криво
    level.zItem_MedBox_temp enableLinkTo();
    
    level.zItem_MedBox = spawn("script_model", level.zItem_MedBox_temp.origin);
    level.zItem_MedBox.angles = self.angles + (0, 90, 0);
    level.zItem_MedBox.health = 9999;
    level.zItem_MedBox.maxhealth = 9999;
    level.zItem_MedBox setModel("com_plasticcase_enemy");
    level.zItem_MedBox cloneBrushmodelToScriptmodel(level.airDropCrateCollision);
    level.zItem_MedBox setCanDamage(true);
    level.zItem_MedBox physicsLaunchServer((0, 0, 0), (0, 0, 0));
    
    level.zItem_MedBox_temp delete();
    
    level.zItem_MedBox thread medbox_damagehealing();
    level.zItem_MedBox thread medbox_delete();
    
    foreach(player in level.players) // fix: box recreating while someone is healing
        player.zHealth_ProcessHealing = false;
}

medbox_damagehealing()
{
    self endon("zbuilding_delete");
    
    for(;;) {
        self waittill("damage", damage, attacker, direction_vec, point, type, modelName, tagName, partName, iDFlags);
        
        if(attacker.health >= attacker.maxhealth)
            continue;
        
        if(type != "MOD_MELEE")
            continue;
        
        // do this to not allow kill medbox
        self.health = self.maxhealth;
        
        attacker thread maps\mp\gametypes\_damagefeedback::updateDamageFeedback("zheal");
        
        self thread medbox_healing(attacker);
    }
}

medbox_healing(player)
{
    if(player.zHealth_ProcessHealing)
        return;
    
    player endon("death");
    player endon("disconnect");
    //self endon("zbuilding_delete");
    
    player.zHealth_ProcessHealing = true;
    player _disableWeaponSwitch();
    player _disableOffhandWeapons();
    player thread medbox_fix_player_on_death();
    player giveWeapon("onemanarmy_mp", 0, false);
    wait(0.1); player switchToWeapon("onemanarmy_mp");
    
    player iPrintLn("^2Healing...");
    
    req_health = player.maxhealth - player.health;
    tick = 0;
    
    while(tick < level.zCfg_PlayerHealingTime) {
        wait(1.0);
        player.health += int(req_health/level.zCfg_PlayerHealingTime);
        
        tick++;
    }
    
    if(tick < level.zCfg_PlayerHealingTime) // player interrupted healing
        player iPrintLn("^1Healing interrupted!");
    else
        player.health = player.maxhealth;
    
    wait(0.1); player switchToWeapon(player getLastWeapon());
    player takeWeapon("onemanarmy_mp");
    player _enableWeaponSwitch();
    player _enableOffhandWeapons();
    player.zHealth_ProcessHealing = false;
    
    player notify("zhealed");
}

medbox_fix_player_on_death()
{
    self endon("disconnect");
    self endon("zhealed");
    
    self waittill("death");
    
    self _enableWeaponSwitch();
    self _enableOffhandWeapons();
}

medbox_delete()
{
    self waittill("zbuilding_delete");
    wait(0.05);
    
    self delete();
}

